package cn.itsource.controller;

import cn.itsource.domain.Ip;
import cn.itsource.query.IpQuery;
import cn.itsource.service.IpService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 接口类：IP地址
 */
@RestController
@RequestMapping("/ip")
public class IpController {

    @Autowired
    private IpService ipService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Ip> findAll(){
        return ipService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Ip findOne(@PathVariable("id")Long id){
        return ipService.findOne(id);
    }

    /**
     * 添加和修改
     * @param ip
     * @param session
     * @return
     */
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Ip ip, HttpSession session){
        try {
            if(ip.getId()==null){//添加操作
                ipService.add(ip);
            }else{
                ipService.update(ip);
            }
            //如果成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            ipService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 分页查询 + 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageInfo<Ip> pageQuery(@RequestBody IpQuery query){
        return ipService.pageQuery(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){  //["2","14"] - Long[] ids
        try {
            ipService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    /**
     * 前台：查询是否点过赞
     * @param articleId
     * @param request
     * @return
     */
    @PutMapping("/isStar/{articleId}")
    public AjaxResult isStar(@PathVariable("articleId")Long articleId, HttpServletRequest request){
        String address = request.getRemoteAddr();
        Ip ip = new Ip();
        ip.setAddress(address);
        ip.setArticleId(articleId);
        return ipService.isStar(ip);
    }

    /**
     * 点赞
     * @param articleId
     * @param request
     * @return
     */
    @PutMapping("/starNum/{articleId}")
    public AjaxResult starNum(@PathVariable("articleId")Long articleId, HttpServletRequest request){
        String address = request.getRemoteAddr();
        Ip ip = new Ip();
        ip.setAddress(address);
        ip.setArticleId(articleId);
        return ipService.starNum(ip);
    }

}
